package com.cg.cgms.controller;


import com.cg.cgms.pojo.OpenId;
import com.cg.cgms.pojo.Result;

import com.cg.cgms.pojo.Shop;
import com.cg.cgms.pojo.User;
import com.cg.cgms.service.ShopService;
import com.cg.cgms.service.UserService;
import com.cg.cgms.utils.JwtUtil;

import com.cg.cgms.utils.ThreadLocalUtil;
import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final String APPID = "wxd11d985ae182b4e9";//wxf9c6d94fe38e43e5
    private static final String SECRET = "0c1f3c25238f8d2cb10ce79189ad83ea";//3b7bc0ea41f303e422ad8f06e0e9cf8a
    private static final String WECHAT_API_URL = "https://api.weixin.qq.com/sns/jscode2session";

    @Autowired
    private UserService userService;
    @Autowired
    private ShopService shopService;

    @GetMapping("/login")
    public Result<String> login(@RequestParam("code") String code){
        if(code==null){
            return Result.error("失败");
        }
        RestTemplate restTemplate = new RestTemplate();
        String urlTemplate = WECHAT_API_URL +
                "?appid={appid}&secret={secret}&js_code={code}&grant_type=authorization_code";
        // String code="0b1zLHFa12ge1H0rMTHa1x99bP0zLHFE";
        String response = restTemplate.getForObject(urlTemplate, String.class, APPID, SECRET, code);
        // 响应示例：{"session_key":"xxx","openid":"oWgGz4xxxxxx"}
        //System.out.println(response);
        Gson gson = new Gson();
        OpenId person = gson.fromJson(response, OpenId.class);
        //System.out.println(person.getOpenid());
        String openid=person.getOpenid();

        //根据openid查询用户
        User loginUser=userService.fidByOpenid(openid);

        //判断是否存在用户
        if(loginUser==null){
            userService.insert(openid);
            loginUser=userService.fidByOpenid(openid);
        }
        Map<String,Object> claims=new HashMap<>();
        claims.put("openid",loginUser.getOpenid());
        //生成token
        String token=JwtUtil.genToken(claims);
        //jwt令牌
        return Result.succsee(token);
    }
    @GetMapping("/userInfo")
    public Result<User> userInfo(/*@RequestHeader(name = "Authorization") String token*/){
        /*//根据用户名查询用户
        Map<String, Object> map = JwtUtil.parseToken(token);
        String username=(String) map.get("username");*/
        Map<String, Object> map = ThreadLocalUtil.get();
        String openid=(String) map.get("openid");
        User user=userService.fidByOpenid(openid);
        return Result.succsee(user);
    }
    //查询收藏店铺
    @GetMapping("/findcollect")
    public Result findcollect(){

        Map<String, Object> map = ThreadLocalUtil.get();
        String openid=(String) map.get("openid");
        System.out.println(openid);
        ArrayList<Integer> ssid=userService.find(openid);
        if(ssid==null){
            return Result.error("没有收藏");
        }
        ArrayList<Shop> shops=new ArrayList<>();
        for(Integer list:ssid){
            shops.add(shopService.findBySsid(list));
        }
        return Result.succsee(shops);
    }

    //添加收藏的店铺
    @PostMapping("/collect")
    public Result collect(@RequestBody Map<String, Integer> requestBody){
        Integer ssid = requestBody.get("ssid");
        Map<String, Object> map = ThreadLocalUtil.get();
        String openid=(String) map.get("openid");
        if(userService.find(openid,ssid)){
            userService.addCollect(openid,ssid);
            return Result.succsee();
        }

        return Result.error("已经被添加");
    }
    @DeleteMapping("/deleteCollect")
    public Result deleteCollect(@RequestBody Map<String, Integer> requestBody){
        Integer ssid = requestBody.get("ssid");
        Map<String, Object> map = ThreadLocalUtil.get();
        String openid=(String) map.get("openid");
        System.out.println(ssid);
        System.out.println(openid);
        if(!userService.find(openid,ssid)){
            userService.deleteCollect(openid,ssid);
            return Result.succsee();
        }

        return Result.error("不存在");
    }



}
